<?php

namespace app\data\controller\channel;

use app\data\model\DataWechat;
use think\admin\Controller;
use app\data\model\ShopOrder;
use think\admin\helper\QueryHelper;

/**
 * 视频号管理
 * Class Mark
 * @package app\data\controller\news
 */
class Vedionumber extends Controller
{
    /**
     * 视频号管理
     * @auth true
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        DataWechat::mQuery()->layTable(function () {
            $this->title = '视频号管理';
        }, function (QueryHelper $query) {
            $query->where(['channel' => 2]);
            $query->like('name')->dateBetween('created_at');
        });
    }

    /**
     * 添加视频号
     * @auth true
     */
    public function add()
    {
        DataWechat::mForm('form');
    }

    /**
     * 编辑视频号
     * @auth true
     */
    public function edit()
    {
        DataWechat::mForm('form');
    }

    /**
     * 订单列表处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function _index_page_filter(array &$data)
    {
        foreach ($data as &$v){
            $v['not_settlement_amount'] = 0;
            $amount = ShopOrder::info(array(
                'channel_code'  => $v['code'],
                'has_settle_account'=>0
            ),"sum(payment_amount) as payment_amount");
            $v['not_settlement_amount'] = $amount['payment_amount']/100??0;
            $v['settlement_amount'] = $v['settlement_amount']/100??0;
            $v['withdraw_amount'] = $v['withdraw_amount']/100??0;
        }
    }
    /**
     * 表单数据处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function _form_filter(array &$data)
    {
        if ($this->request->isPost()) {
            //校验是否存在
            $data['channel'] = 2;
            $info = DataWechat::info(array(
                'appid' => $data['appid'],
                'channel'=>2
            ),'id');
            if(empty($info) === FALSE){
                if(isset($data['id'])  && empty($data['id']) === FALSE){
                    if($info['id'] != $data['id']){
                        $this->error("信息有误，请确认！");
                    }
                }
            }
            //生成带参数二维码
            if(!isset($data['id'])  || empty($data['id'])){
                $data['created_at'] = \Tools::getDateTimeByTime();
                $salt = rand(1000,9999);
                $data['code'] = \Tools::SHA256($data['appid'].$salt);
                $data['check_pwd'] = $data['code'];
                $data['salt'] = $salt;
                //生成带参数二维码
                $data['qrcode_path'] = \Common::createQrcode(array(
                    'channel_code'      => $data['code'],
                    'channel'           =>1
                ),1);
            }else{
                unset($data['appid']);
            }
            $data['updated_at'] = \Tools::getDateTimeByTime();
        }
    }
    /**
     * 表单结果处理
     * @param bool $state
     * @return void
     */
    protected function _form_result(bool $state)
    {
        if ($state) {
            $this->success('成功', "javascript:$('#TagsData').trigger('reload')");
        }
    }

    /**
     * 删除视频号
     * @auth true
     */
    public function remove()
    {
        DataWechat::mDelete();
    }
}
